package com.tiancheng.trade.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.order.mapper.AbnormalQrPayOrderMapper;
import com.tiancheng.trade.order.model.AbnormalQrPayOrder;
import com.tiancheng.trade.order.service.AbnormaQrlPayOrderService;
import com.tiancheng.trade.commom.core.utils.DateTimeUtil;
import com.tiancheng.trade.order.vo.admin.QueryAbnormalQrPayOrderVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 异常支付订单service接口 impl
 *
 * @author likailun
 * @date 2021/9/7
 */
@Service
public class AbnormalQrPayOrderServiceImpl implements AbnormaQrlPayOrderService {

    @Resource
    private AbnormalQrPayOrderMapper abnormalQrPayOrderMapper;

    @Override
    public int save(AbnormalQrPayOrder abnormalQrPayOrder) {
        abnormalQrPayOrder.setCreatedDt(LocalDateTime.now());
        return this.abnormalQrPayOrderMapper.insert(abnormalQrPayOrder);
    }

    @Override
    public List<AbnormalQrPayOrder> queryByOrderId(String orderId) {
        QueryWrapper<AbnormalQrPayOrder> conditions = new QueryWrapper<>();
        conditions.eq("order_id",orderId);
        return this.abnormalQrPayOrderMapper.selectList(conditions);
    }

    @Override
    public PageInfo<AbnormalQrPayOrder> queryPage(QueryAbnormalQrPayOrderVO params) {
        QueryWrapper<AbnormalQrPayOrder> conditions = new QueryWrapper<>();
        if(params.getId()!=null){
            conditions.eq("id",params.getId());
        }
        if(StringUtils.isNotEmpty(params.getUid())){
            conditions.eq("uid",params.getUid());
        }
        if(StringUtils.isNotEmpty(params.getOrderId())){
            conditions.eq("order_id",params.getOrderId());
        }
        if(StringUtils.isNotEmpty(params.getFlowId())){
            conditions.eq("flow_id",params.getFlowId());
        }
        if(StringUtils.isNotEmpty(params.getQrNo())){
            conditions.eq("qr_no",params.getQrNo());
        }
        if(StringUtils.isNotEmpty(params.getMerchantName())){
            conditions.eq("merchant_name",params.getMerchantName());
        }
        if(StringUtils.isNotEmpty(params.getMerchantId())){
            conditions.eq("merchant_id",params.getMerchantId());
        }

        if(StringUtils.isNotEmpty(params.getVoucherNum())){
            conditions.eq("voucher_num",params.getVoucherNum());
        }
        if (StringUtils.isNotEmpty(params.getCreatedDtBegin()) && StringUtils.isNotEmpty(params.getCreatedDtEnd())) {
            conditions.between("created_dt",
                    DateTimeUtil.format(params.getCreatedDtBegin() + " 00:00:00", "yyyy-MM-dd HH:mm:ss"),
                    DateTimeUtil.format(params.getCreatedDtEnd() + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }
        return abnormalQrPayOrderMapper.find(conditions, params.getCurrentPage(), params.getPageSize());
    }
}
